/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.positioning;

import java.awt.Color;
import java.awt.Component;
import java.awt.dnd.DropTarget;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import ontocompo.ui.screenmanagement.Screen;
import ontocompo.ui.screenmanagement.ScreenConsumer;
import ontocompo.ui.screenmanagement.ScreenPossibilities;

public class PositioningPane extends JPanel implements ScreenConsumer{

	private ArrayList<Screen> availabledScreens;
	
	private JComboBox screenComboBox;
	private JPanel loadedScreenPane;
	private JPanel inBulkPane;
	private JPanel buildPane;
	
	public static Screen selectedScreen;
	
	private DragManager dragManager;
	
	public PositioningPane(ArrayList<Screen> availabledScreens) {
		super();
		this.availabledScreens = availabledScreens;
		this.dragManager = new DragManager();
		
		this.build();
	}
	
	public void build() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		ScreenPossibilities ep = new ScreenPossibilities(false, null, true, this);
		ep.setButtonMessage("select");
		ep.init();
		ep.updateScreensList(availabledScreens);
		add(ep);
		
		/*
		screenComboBox= new JComboBox();
		
		for(Screen s : this.availabledScreens) {
			screenComboBox.addItem(s);
			s.setDragManager(this.dragManager);
		}
		
		
		if(this.availabledScreens.size() > 0) {
			screenComboBox.setSelectedItem(this.availabledScreens.toArray()[0]);
		}
		
		
		screenComboBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				// TODO Auto-generated method stub
				loadSelectedScreen();
			}
			
		});
		
		this.add(screenComboBox);
		*/
		
		loadedScreenPane = new JPanel();
		loadedScreenPane.setLayout(new BoxLayout(loadedScreenPane, BoxLayout.X_AXIS));
		this.add(loadedScreenPane);
		
		inBulkPane = new JPanel();
		inBulkPane.setLayout(new BoxLayout(inBulkPane, BoxLayout.Y_AXIS));
		loadedScreenPane.add(inBulkPane);
		
		buildPane = new JPanel();
		buildPane.setBackground(Color.WHITE);
		buildPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
//		buildPane.addMouseListener(dragManager);
		
		loadedScreenPane.add(buildPane);
		
		this.setDropTarget(new DropTarget(this, new PositioningDropTarget()));
		
		// this.loadSelectedScreen();
	}
	
	private void loadSelectedScreen(Screen newSelected) {
		if(PositioningPane.selectedScreen != null) {
			for(Component jc : PositioningPane.selectedScreen.getInBulkComponents()) {
				jc.removeMouseListener(this.dragManager);
			}
		}
		
//		PositioningPane.selectedScreen = (Screen) this.screenComboBox.getSelectedItem();
		PositioningPane.selectedScreen = newSelected ; // (Screen) this.screenComboBox.getSelectedItem();
		
		if(PositioningPane.selectedScreen != null) {
			this.buildPane.removeAll();
			this.inBulkPane.removeAll();
			
			if(PositioningPane.selectedScreen.getRootScreen() != null) {
				this.buildPane.add(PositioningPane.selectedScreen.getRootScreen());
			}
			ArrayList<Component> inBulkComponents = PositioningPane.selectedScreen.getInBulkComponents();
			
			for(Component c: inBulkComponents) {
				JComponent jc = (JComponent) c;
				jc.setTransferHandler(new JComponentTransfertHandler());
				jc.addMouseListener(this.dragManager);
				this.inBulkPane.add(jc);
			}
			
			this.validate();
		}
	}
	
	public void transfertJComponent(JComponent toMove) {
		this.inBulkPane.remove(toMove);
		PositioningPane.selectedScreen.removeFromBulkList(toMove);
		
		PositioningPane.selectedScreen.setRootScreen(new GridRelativePane(toMove));
		
		if(this.buildPane.getComponents().length == 0) {
			this.buildPane.add(PositioningPane.selectedScreen.getRootScreen());
		}
		
		this.validate();
	}
	
	public String toString() {
		return "the positioning pane";
	}

	@Override
	public void consume(Screen s) {
		loadSelectedScreen( s);
		
	}
}
